package com.qfmy.project.controller;

import com.qfmy.common.pojo.TestUser;
import com.qfmy.common.pojo.TokenVo;
import com.qfmy.common.utils.JwtUtils;
import com.qfmy.common.utils.RedisUtils;
import com.qfmy.common.utils.StringUtils;
import com.qfmy.core.base.service.TokenService;
import com.qfmy.core.pojo.entity.ErrorCode;
import com.qfmy.core.pojo.entity.ResultMessage;
import com.qfmy.core.pojo.exception.CustomException;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: TestController
 * @author： zhengjt
 * @data： 2021/9/11 15:07
 * @version：1.0
 * @描述： 测试使用的controller
 */
@Log4j2
@RestController
public class TestController {

//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    private TokenService tokenService;

    /**
     * 普通接口测试（测试程序是否能够走通）
     * @return
     */
    @ResponseBody
    @GetMapping("/test")
    public Object test(){
        log.error("===========test============");
        return "Hello World !";
    }

    /**
     * 测试 接口成功后统一返回格式
     * @return
     */
    @ResponseBody
    @GetMapping("/testResultSuccess")
    public Object testResultSuccess(){
        log.info("===========testResultSuccess============");
        return ResultMessage.success("Hello World!");
    }

    /**
     * 测试 接口失败后统一返回格式
     * @return
     */
    @ResponseBody
    @GetMapping("/testResultFail")
    public Object testResultFail() throws Exception {
        log.error("===========testResultFail============");
//        return new ResultMessage(false, ErrorCode.ERROR_BUSINESS.getCode(),ErrorCode.ERROR_BUSINESS.getMessage());
        throw new CustomException(ErrorCode.ERROR_BUSINESS);
//        throw new CustomException();
    }

    /**
     * 测试 接口失败后统一返回格式
     * @return
     */
    @ResponseBody
    @GetMapping("/testResultFailMessage")
    public Object testResultFailMessage() throws Exception {
        log.error("===========testResultFailMessage============");
//        return new ResultMessage(false, ErrorCode.ERROR_BUSINESS.getCode(),ErrorCode.ERROR_BUSINESS.getMessage());
//        throw new CustomException(ErrorCode.ERROR_BUSINESS);
        throw new CustomException("手动测试信息 | （test）");
    }

    @ResponseBody
    @GetMapping("/testUser")
    public Object testUser(TokenVo testUser) throws Exception{
        if (ObjectUtils.isEmpty(testUser)){
            throw new CustomException(ErrorCode.NEVER_USER);
        }
        log.info("TestUser信息 : \n {}",testUser.toString());
        return testUser;
    }

    @ResponseBody
    @PostMapping("/testAddRedis")
    public Object testAddRedis(TokenVo object) throws Exception {
        log.info(" =========== 进入 testAddRedis ============= ");
        if (object == null){
            throw new CustomException(ErrorCode.NULL_PARAM);
        }
        if (StringUtils.isNull(object.getUserName())){
            throw new CustomException(ErrorCode.NULL_PARAM);
        }
        log.info("user 数据查看 ： \n {} ", object.toString());
//        stringRedisTemplate.opsForValue().set(object.getUserName(), object.toString());
        boolean redisSet = redisUtils.set(object.getUserName(), object.toString());
        if (!redisSet){
            return "false";
        }
        return "success";
    }

    @ResponseBody
    @GetMapping("/testGetRedis")
    public Object testGetRedis(String keys) throws Exception {
//        Object object = stringRedisTemplate.opsForValue().get(keys);
        Object object = redisUtils.get(keys);
        return  object;
    }

    @ResponseBody
    @PostMapping("/testDeleteRedis")
    public Object testDeleteRedis(String keys) throws Exception {
//        stringRedisTemplate.delete(keys);
        redisUtils.delete(keys);
        return  "";
    }

    @ResponseBody
    @GetMapping("/test/getToken")
    public Object testGetToken(){
        TokenVo object = new TokenVo();
        object.setUserId("000001");
        object.setUserName("test");
        object.setUserNick("测试 - 开发者");
        object.setDeptId("000000001");
        object.setDeptName("测试部门");
        object.setCorpId("000000001");
        object.setCorpName("测试Corp");
        object.setOrgId("000000001");
        object.setOrgName("测试Org");
        String token = tokenService.createToken(object);
//        stringRedisTemplate.opsForValue().set(object.getUserName(), token);
//        redisUtils.set(object.getUserName(), token);
        return token;
    }



}
